package com.example.shortmessage.adapter;


import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shortmessage.R;
import com.example.shortmessage.contract.MessageContract;
import com.example.shortmessage.data.constant.DetailsConst;
import com.example.shortmessage.data.entity.MessageData;
import com.example.shortmessage.interfaces.DialogOnClickInterface;
import com.example.shortmessage.tips.dialog.NormalDialog;
import com.example.shortmessage.ui.activity.DetailsActivity;
import com.example.shortmessage.utils.DateUtils;

import java.util.List;

public class MyMessageAdapter extends RecyclerView.Adapter<MyMessageAdapter.ViewHolder> implements DialogOnClickInterface {
    private List<MessageData> messageDataLists;
    private Activity context;
    private MessageContract.Presenter mPresenter;
    private MessageData data;

    public MyMessageAdapter(List<MessageData> messageDataLists, Activity context, MessageContract.Presenter mPresenter) {
        this.messageDataLists = messageDataLists;
        this.context = context;
        this.mPresenter = mPresenter;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_message, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MessageData messageData = messageDataLists.get(position);
        String name = messageData.getName();
        String body = messageData.getBody();
        if(name==null) {
            if(body.startsWith("【")) {
                name = body.substring(1,body.indexOf("】"));
                if(name.contains(" ")) {
                    name = body.substring(0,body.indexOf(" "));
                }
                name = name.replace("【","");
            }else {
                name = messageData.getNumber();
            }
        }
        holder.tv_title.setText(name);
        holder.tv_date.setText(DateUtils.dateToString(messageData.getDate(),"yyyy年MM月dd日HH时mm分"));
        holder.tv_content.setText(body);
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, DetailsActivity.class);
            intent.putExtra(DetailsConst.SIGN,messageData.getNumber());
            context.startActivity(intent);
            mPresenter.upData(messageData.getSms_id()+"");
            context.finish();
        });
        holder.itemView.setOnLongClickListener(v -> {
            data = messageData;
            NormalDialog.getInstance(context,this)
                    .createDialog("删除短信","确认删除该条信息","确认删除");
            return false;
        });
    }

    @Override
    public int getItemCount() {
        return messageDataLists==null ? 0 : messageDataLists.size();
    }

    @Override
    public void interfacePositive() {
        mPresenter.delete(data);
        messageDataLists.remove(data);
        this.notifyDataSetChanged();
    }

    @Override
    public void interfaceNegative() {

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        TextView tv_date;
        TextView tv_content;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_name);
            tv_date = itemView.findViewById(R.id.tv_date);
            tv_content = itemView.findViewById(R.id.tv_content);
        }

    }
}
